package com.xavier.controller;

import com.xavier.dto.DeptDTO;
import com.xavier.dto.DeptQueryDTO;
import com.xavier.result.Result;
import com.xavier.service.IDeptService;
import com.xavier.vo.DeptDetailVO;
import com.xavier.vo.DeptPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 请求处理层-部门管理类
 *
 * @author Xavier
 * @version 1.0
 */
@Slf4j
@RequestMapping("dept")
@RestController
@Tag(name = "部门管理接口")
public class DeptController extends CommonController {

    @Resource
    private IDeptService deptService;

    @Operation(summary = "新增部门", description = "新增部门")
    @PutMapping("add")
    public Result add(@Validated @RequestBody DeptDTO dto) {
        deptService.createDept(dto);
        return Result.success();
    }

    @Operation(summary = "编辑部门", description = "编辑部门")
    @PutMapping("update")
    public Result update(@Validated @RequestBody DeptDTO dto) {
        deptService.updateDept(dto);
        return Result.success();
    }

    @Operation(summary = "删除部门", description = "删除部门")
    @DeleteMapping("delete/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<Integer> delete(@PathVariable Long id) {
        deptService.deleteDept(id);
        return Result.success();
    }

    @Operation(summary = "获取部门信息", description = "根据部门id获取部门信息")
    @GetMapping("get/{id}")
    @Parameter(name = "id", description = "id", required = true)
    public Result<DeptDetailVO> get(@PathVariable Long id) {
        return Result.success(deptService.getDept(id));
    }

    @Operation(summary = "获取部门树形信息", description = "获取部门树形信息")
    @PostMapping("getList")
    public Result<List<DeptPageVO>> getList(@Validated @RequestBody DeptQueryDTO dto) {
        return Result.success(deptService.getList(dto));
    }

}
